﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StudentManagerUI
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        #region 窗体移动代码
        private Point mouseOff;//鼠标移动位置变量
        private bool leftFlag;//标签是否为左键
        private void Frm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                leftFlag = true;                  //点击左键按下时标注为true;
            }
        }
        private void Frm_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }
        private void Frm_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后标注为false;
            }
        }

        #endregion
        #region 窗体嵌入子窗体代码
        private void OpenForm(Form childForm)
        {
            //添加的空间都会放入Controls集合中，所有从panel容器的controls的中取出
            foreach (Control control in this.panel2.Controls)
            {
                if (control is Form)
                {
                    ((Form)control).Close();
                }
            }

            childForm.TopLevel = false;//子窗体设置为非顶级控件，子窗体将受到父窗体的布局和生命周期管理，这些窗体不会出现在任务栏中，而是作为父窗体的一部分显示
            childForm.Parent = this.panel2;//设置窗体容器
            childForm.Dock = DockStyle.Fill;//随着容器大小自动调整子窗体大小

            childForm.Show();
        }

        #endregion

        //显示添加新学员窗体
        private void btnAddStu_Click(object sender, EventArgs e)
        {
            OpenForm(new FrmAddStudent());
        }

        //显示学员管理窗体
        private void btnStuManage_Click(object sender, EventArgs e)
        {   
            OpenForm(new FrmStudentManage());
        }
        //考勤打卡
        private void btnCard_Click(object sender, EventArgs e)
        {
            OpenForm(new FrmAttendance());
        }
        //考勤查询
        private void btnAttendanceQuery_Click(object sender, EventArgs e)
        {
            OpenForm(new FrmAttendanceQuery()); 
        }
        //成绩浏览
        private void btnScoreQuery_Click(object sender, EventArgs e)
        {
            OpenForm(new FrmScoreQuery());
        }
        //成绩分析
        private void btnScoreAnalasys_Click(object sender, EventArgs e)
        {
            OpenForm(new FrmScoreManage());
        }
        //密码修改
        private void btnModifyPwd_Click(object sender, EventArgs e)
        {
            new FrmModifyPwd().ShowDialog();
        }
        //账号切换
        private void btnChangeAccount_Click(object sender, EventArgs e)
        {
            //创建登录窗体
            FrmUserLogin objUserLogin = new FrmUserLogin();
            objUserLogin.Text = "[账号切换]";
            DialogResult result = objUserLogin.ShowDialog();

            //根据窗体返回值判断用户登录是否成功
            if (result == DialogResult.OK)
            {
                this.lblCurrentUser.Text = Program.currentAdmin.AdminName + "]";
            }
        }
        //批量导入学生信息
        private void btnImportStu_Click(object sender, EventArgs e)
        {
            OpenForm(new FrmImportData());
        }

        #region 菜单栏按钮

        private void tmiClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tmiModifyPwd_Click(object sender, EventArgs e)
        {
            btnModifyPwd_Click(null, null);
        }

        #endregion

        private void tsmiAddStudent_Click(object sender, EventArgs e)
        {
            btnAddStu_Click(null, null);
        }

        private void tsmi_Import_Click(object sender, EventArgs e)
        {
            btnImportStu_Click(null, null);
        }

        private void tsmiManageStudent_Click(object sender, EventArgs e)
        {
            btnStuManage_Click(null, null);
        }

        private void tsmiQuery_Click(object sender, EventArgs e)
        {
            btnScoreQuery_Click(null,null);
        }
        private void tsmiQueryAndAnalysis_Click(object sender, EventArgs e)
        {
            btnScoreAnalasys_Click(null, null);
        }

        private void tsmi_Card_Click(object sender, EventArgs e)
        {
            btnCard_Click(null, null);
        }

        private void tsmi_AQuery_Click(object sender, EventArgs e)
        {
            btnAttendanceQuery_Click(null, null);
        }
    }
}
